<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      class="tab-control"
      v-show="isTabFixed"
    ></tab-control>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImagesLoad="swiperImagesLoad"
      ></home-swiper>
      <home-recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goods-list :goods="showGoods" />
    </scroll>
    <!-- native监听组件点击 -->
    <back-top @click.native="backClick" v-show="isShow" />
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import TabControl from "../../components/content/tabControl/TabControl.vue";
import GoodsList from "../../components/content/goods/GoodsList.vue";
import Scroll from "../../components/common/scroll/Scroll.vue";

import HomeSwiper from "./childComps/HomeSwiper.vue";
import HomeRecommendView from "./childComps/HomeRecommendView.vue";
import FeatureView from "./childComps/FeatureView.vue";

// import NavBar from '../../components/common/navbar/NavBar.vue'
import { getHomeMultidata, getHomeGoods } from "network/home";
import { itemListenerMixin, backTopMixin } from "common/mixin";

export default {
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",

      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  mixins: [itemListenerMixin, backTopMixin],
  mounted() {
    // 1.图片加载完成的事件监听
    //3.监听item中图片加载完成
    //  this.$bus.$on('itemImageLoad', () => {
    //    console.log('1```````');
    //   this.$refs.scroll.refresh()
    //  })
    //  const refresh = debounce(this.$refs.scroll.refresh, 50)
    //  this.itemListen = () => {
    //     refresh()}
    //    this.$bus.$on('itemImageLoad', this.itemListen)
    //2.获取tabControl的offsetTop
    //所有组件都有一个属性 $el: 用于获取组件中的元素
    // console.log(this.$refs.tabControl.$el.offsetTop);
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  //进入组件加载完成回调的函数
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0);

    //回来的时候对scroll做一次刷新
    this.$refs.scroll.refresh();
  },
  //离开组件的时候回调的函数
  deactivated() {
    //保存组件离开的时候的位置，传给activeted 保证回来的时候显示的是原来离开时候的位置
    this.saveY = this.$refs.scroll.getScrollY();
    //取消全局事件的监听
    this.$bus.$off("itemImageLoad", this.homeItemListen);
  },
  created() {
    //1.请求多个数据
    this.getHomeMultidata();

    //  console.log(this.getHomeMultidata);
    //2.请求商品数据
    this.getHomeGoods("pop");
    //  console.log();
    //  console.log( this.getHomeGoods('pop'));
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  methods: {
    //时间监听相关的方法
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },

    //监听滚动的位置
    contentScroll(position) {
      //  console.log(position);
      //  if(position.y < -1000){
      //    this.isShow = true
      //  }else{
      //    this.isShow = false

      //  }
      //1.判断backtop是否显示
      this.isShow = position.y < -1000;
      //2.判断tabcontrol是否吸顶(position:fiexd)
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    //下拉加载更多的方法
    loadMore() {
      //  console.log('sss');
      this.getHomeGoods(this.currentType);
    },
    //监听图片加载完成时候tabcontrol距离顶部的offsettop值
    swiperImagesLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    //网络请求相关的方法
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // console.log(res);
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
        // console.log(this.recommends);
      });
    },

    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        //完成上拉加载更多
        if (this.$refs.scroll.finishPullUp() !== undefined) {
          this.$refs.scroll.finishPullUp();
        }
        // console.log(this.$refs.scroll.finishPullUp);
        // console.log(this.$refs.scroll);
      });
    },
  },
};
</script>

<style scoped>
#home {
  /* padding-top:44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* 使用浏览器原生滚动时，为了让浏览器不滚动不跟随 现在用了scroll就不需要了 */
  /* position: fixed;
  left: 0;
  right: 0;
  top: 0;
   z-index: 9;   */
}
/* .fixed{
  position: fixed;
  left: 0;
  right: 0;
  top: 44px;
} */

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  height: calc(100% - 93px);
}
.tab-control {
  position: relative;
  z-index: 9;
}
/* 方法2 使用calc机算的方法 */
/* .content{
  height: calc(100% - 93px);
  overflow: hidden;
  margin-top: 44px;
} */
</style>